// Get achieve type.
// Date 21:18 13/03/2017
// By Ben a.k.a DreamVB

#include <string>
#include <iostream>
#include <time.h>

using namespace std;
using std::cout;
using std::endl;

enum TFileType{
	fBinray = 0,
	fZIP = 1,
	fRAR = 2,
	f7ZIP = 3,
	fCAB = 4
};

char *FileType[5] = { "Binary Format","Zip File","RAR File", "7Zip Format","MS CAB Format"};

int WhatIsIT(int *data){
	//Check for ZIP
	if ((((data[0] == 80) && (data[1] == 75) && (data[2] == 3) && (data[3] == 4)))){
		return fZIP;
	}
	//Check for RAR
	if ((((data[0] == 82) && (data[1] == 97) && (data[2] == 114) && (data[3] == 33)))){
		return fRAR;
	}
	//Check for 7ZIP
	if ((((data[0] == 55) && (data[1] == 122) && (data[2] == 188) && (data[3] == 175)))){
		return f7ZIP;
	}
	//Check for CAB
	if ((((data[0] == 77) && (data[1] == 83) && (data[2] == 67) && (data[3] == 70)))){
		return fCAB;
	}
	return fBinray;
}

int main(int argc, char *argv[])
{

	int header[4] = { 0 };
	FILE *fp = NULL;

	if (argc != 2){
		cout << "USAGE " << argv[0] << " <Filename>" << endl;
		exit(1);
	}

	//Try and open the filename.
	fp = fopen(argv[1], "rb");

	if (!fp){
		cout << "Cannot open source file: " << endl << argv[1] << endl;
		exit(1);
	}

	//Read in 4 bytes of the file.
	header[0] = fgetc(fp);
	header[1] = fgetc(fp);
	header[2] = fgetc(fp);
	header[3] = fgetc(fp);

	int id = WhatIsIT(header);

	cout << "File Format ID : " << id << endl;
	cout << "FileType       : " << FileType[id] << endl;

	//Close file
	fclose(fp);

	return EXIT_SUCCESS;
}